package com.bill99.mgw.receive;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.bill99.mgw.entity.TransInfo;
import com.bill99.mgw.util.ParseUtil;
import com.elite.EliteService;

public class ReceiveTR3ToTR4 extends HttpServlet {
	private static final Log log=LogFactory.getLog(ReceiveTR3ToTR4.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * ����doPost()����
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * �÷�����4����TR3�����TR4
	 */
	@SuppressWarnings( { "unused", "unchecked" })
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//����������Ϣ���ַ����
		System.out.println("-----------------ReceiveTR3ToTR4-----------------------");
		request.setCharacterEncoding("utf-8");
		InputStream is = request.getInputStream();
		if (is != null) {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			byte[] receiveBuffer = new byte[2048];
			int readBytesSize = is.read(receiveBuffer);
			while (readBytesSize != -1) {
				bos.write(receiveBuffer, 0, readBytesSize);
				readBytesSize = is.read(receiveBuffer);
			}
			String reqData = new String(bos.toByteArray(), "UTF-8");
			log.info("返回TR3:"+reqData);
			//����TR3��ĵ�һ���־�ֶ�
			TransInfo.setRecordeText_1("TxnMsgContent");
			//����TR3��Ĵ����־�ֶ�
			TransInfo.setRecordeText_2("ErrorMsgContent");
			//�������Ľ���ʽ
			TransInfo.setFLAG(true);

			/***************��ʼ����TR3**************/
			//����ȡ����ݴ���DOM��������
			HashMap respXml = ParseUtil.parseXML(reqData);
			if (respXml != null) {
				//�ӿڰ汾�ţ�version��
				String version = (String) respXml.get("version");

				//�������ͱ��루txnType��
				String txnType = (String) respXml.get("txnType");

				//��Ϣ״̬��interactiveStatus��
				String interactiveStatus = (String) respXml.get("interactiveStatus");

				//���׽�amount��
				String amount = (String) respXml.get("amount");

				//�̻����		  
				String merchantId = (String) respXml.get("merchantId");

				//�̻����		  
				String settleMerchantId = (String) respXml.get("settleMerchantId");

				//�ն˱�ţ�terminalId��
				String terminalId = (String) respXml.get("terminalId");

				//�ⲿ����ο��ţ�externalRefNumber��
				String externalRefNumber = (String) respXml.get("externalRefNumber");

				//�ͻ��ţ�customerId��
				String customerId = (String) respXml.get("customerId");

				//����ο��ţ�refNumber��
				String refNumber = (String) respXml.get("refNumber");

				//Ӧ���루responseCode��
				String responseCode = (String) respXml.get("responseCode");

				//Ӧ���ı���Ϣ��responseTextMessage��
				//		String responseTextMessage=(String)respXml.get("responseTextMessage");

				//���״���ʱ�䣨transTime��
				String transTime = (String) respXml.get("transTime");

				//�ͻ��˽���ʱ�䣨entryTime��
				String entryTime = (String) respXml.get("entryTime");

				//������֯��ţ�cardOrg��
				String cardOrg = (String) respXml.get("cardOrg");

				//����������ƣ�issuer��
				String issuer = (String) respXml.get("issuer");

				//���Կ��ţ�storableCardNo��
				String storableCardNo = (String) respXml.get("storableCardNo");

				//��Ȩ�루authorizationCode��	
				String authorizationCode = (String) respXml.get("authorizationCode");

				//��������ǩ��signature��
				String signature = (String) respXml.get("signature");

				/********TR3�������*********************************/
				//��Ӧ����responseCode��ֵΪ00ʱ�����׳ɹ�
				if ("00".equals(responseCode)) {
					/***************************************************************
					 * ������ݿ���߼�������������ݿ������¼��
					 **************************************************************/
				}
				/********���TR4************************************/
				StringBuffer tr4XML = new StringBuffer();
				tr4XML.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><MasMessage xmlns=\"http://www.99bill.com/mas_cnp_merchant_interface\"><version>1.0</version><TxnMsgContent>");
				tr4XML.append("<txnType>").append(txnType).append("</txnType>");
				tr4XML.append("<interactiveStatus>TR4</interactiveStatus>");
				tr4XML.append("<merchantId>").append(merchantId).append("</merchantId>");
				if (settleMerchantId != null && "".equals(settleMerchantId)) {
					tr4XML.append("<settleMerchantId>").append(settleMerchantId).append("</settleMerchantId>");
				}
				tr4XML.append("<terminalId>").append(terminalId).append("</terminalId>");
				tr4XML.append("<refNumber>").append(refNumber).append("</refNumber>");
				tr4XML.append("</TxnMsgContent></MasMessage>");
				response.getOutputStream().write(new String(tr4XML).getBytes("UTF-8"));
			}
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init();
	}
}
